import qiankun from 'vite-plugin-qiankun';
import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js';

// 按需引入
import IconsResolver from 'unplugin-icons/resolver';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import {ArcoResolver} from 'unplugin-vue-components/resolvers';
import {fileURLToPath, URL} from 'node:url';
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
	const env = loadEnv(mode, process.cwd());
	return {
		base: env.VITE_APP_ENTRY,
		server: {
			watch: { usePolling: true },
			host: "0.0.0.0",
			hmr: true,
			port: 9003,
			headers: {
				'Access-Control-Allow-Origin': '*', // 主应用获取子应用时跨域响应头
			},
		},
		resolve: {
			alias: {
				'@': fileURLToPath(new URL('./src', import.meta.url)),
			},
			extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'],
		},
		css: {
			preprocessorOptions: {
				scss: {
					silenceDeprecations: ["legacy-js-api"],
				},
			},
		},
		plugins: [
			qiankun("chatManage", {
				useDevMode: true
			}),
			cssInjectedByJsPlugin(),
			vue(),
			vueJsx(),
			AutoImport({
				include: [
					/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
					/\.vue$/,
					/\.vue\?vue/ // .vue
				],
				dirs: ['./src'],
				imports: [
					'vue',
					'vue-router',
					{
						'@/hooks/useI18n': ['useI18n'],
						'@/hooks/useCache': ['useCache'],
						'@/hooks/useMessage': ['useMessage'],
					}
				], // 限定范围为 vue, vue-router
				dts: 'types/auto-import.d.ts', // 自动生成 'auto-import.d.ts'全局声明
				resolvers: [
					ArcoResolver(),
				]
			}),
			Components({
				// 按需导入组件，相关组件声明放置于 components.d.ts
				dts: './types/components.d.ts',
				resolvers: [
					IconsResolver({
						enabledCollections: ['ep']
					}),
					ArcoResolver({
						sideEffect: true
					})
				]
			}),
		],
	}
})
